<script setup>
import { storeToRefs } from 'pinia'
import { useMusicStore } from '../store/music'

const musicStore = useMusicStore()
const { progressVal, duration } = storeToRefs(useMusicStore())

const handleClick = (event) => {
  musicStore.progressVal = 0
  const rect = event.offsetX / document.querySelector('.music-progress').clientWidth
  const currentTime = duration.value * rect
  console.log(currentTime)
  musicStore.updateCurrentTime = currentTime
  musicStore.progressVal = (currentTime / duration) * 100
}
</script>

<template>
  <div class="music-progress" @click="handleClick">
    <div class="progress-bar" :style="{ width: progressVal + '%' }"></div>
  </div>
</template>

<style scoped lang="scss">
.music-progress {
  width: 100%;
  height: 2px;
  background: #dbdbdd;
  z-index: 1;
  cursor: pointer;
  transition: all .5s;
  position: absolute;
  left: 0;
  top: -2px;
  &:hover {
    height: 4px;
    top: -4px;
  }
}

.progress-bar {
  width: 0;
  height: 100%;
  background: #1e90ff;
  transition: all .5s;
}
</style>
